package learn;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by Lenovo
 * DATA:2023/10/19
 **/
public class ScheduleTest {
    public static void main(String[] args) {
        //获取对象
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);//参数是核心线程的数量。
        //延时两秒后开始执行任务，没间隔三秒在执行一次任务。
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println(System.currentTimeMillis());
            //模拟耗时
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },2,3,TimeUnit.SECONDS);
    }
    public static void schedule() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        //延时两秒之后执行任务。
        scheduledExecutorService.schedule(() -> {System.out.println(Thread.currentThread().getName());},2, TimeUnit.SECONDS);
        scheduledExecutorService.shutdown();
    }
}
